package com.sihuatech.coreagent.controller;

import org.apache.catalina.manager.util.SessionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.druid.util.StringUtils;
import com.github.pagehelper.util.StringUtil;
import com.sihuatech.coreagent.core.communication.entity.Response;
import com.sihuatech.coreagent.core.communication.entity.User;
import com.sihuatech.coreagent.service.UserService;
import com.sihuatech.coreagent.until.Consts;

@RestController
@RequestMapping("/coreagent")
public class LoginController {

	private static final Logger logger = LoggerFactory.getLogger(LoginController.class);
	
	@Autowired
	private UserService userService;
	
	 @PostMapping("/login")
	public Response login(@RequestParam(required = true) String userName,
			@RequestParam(required = true) String password) {
		 logger.info( userName + "|" + password);
		User user = userService.getUser(userName);
		if (user == null) {
			return Response.ofFail("用户名不存在");
		}
		if (StringUtils.equals(password, user.getUserPassword())) {
			Response res = new Response(Consts.SUCCESS, "登录成功", user);
			return res;
		}
		return Response.ofFail("用户名与密码不匹配");
	}

}
